package com.jcl.recruit.console.logic;

import com.jcl.core.utils.DateUtils;
import com.jcl.recruit.console.ConsoleVerificationCodeDao;
import com.jcl.recruit.console.model.ConsoleVerificationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName VerificationCodeLogic

 * @Author yyliu
 * @Date 2018/9/18 9:37
 * @Version 1.0
 **/
@Component
public class VerificationCodeLogic {

    @Autowired
    private ConsoleVerificationCodeDao consoleVerificationCodeDao;

    public ConsoleVerificationCode addVerificationCode(String receiver,
                                                       String code,
                                                       Integer type,
                                                       int minute) {
        // 先删除
        delete(receiver, type);

        ConsoleVerificationCode verificationCode = new ConsoleVerificationCode();
        verificationCode.setReceiver(receiver);
        verificationCode.setType(type);
        verificationCode.setCode(code);

        verificationCode.setAbateDate(DateUtils.addMinutes(new Date(), minute));

        consoleVerificationCodeDao.add(verificationCode);

        return verificationCode;
    }

    public ConsoleVerificationCode findOneByType(String receiver, Integer type) {
        return consoleVerificationCodeDao.findOneByType(receiver, type);
    }

    public ConsoleVerificationCode validateTime(String receiver, Integer type, Date abateDate) {
        return consoleVerificationCodeDao.validateTime(receiver, type, abateDate);
    }

    public ConsoleVerificationCode validateCode(String receiver, Integer type, String code) {
        return consoleVerificationCodeDao.validateCode(receiver, type, code);
    }

    public int add(ConsoleVerificationCode verificationCode) {
        return consoleVerificationCodeDao.add(verificationCode);
    }

    public int delete(String receiver, Integer type) {
        return consoleVerificationCodeDao.delete(receiver, type);
    }
}
